package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.ArrayList;
import java.util.List;

import static ecnu.liwaTest.dd.DriverSingleton.getChromeDriver;
import static ecnu.liwaTest.dd.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class GuestPage extends BasePage{
    WebDriver driver = getChromeDriver();

    public GuestPage goGuestPage() {
        {
            WebElement element = driver.findElement(By.cssSelector(".el-button > span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(6) > span")).click();
        waitForWindow(1000);
        assertThat(driver.getTitle(), is("访客管理"));
        return this;
    }

    public GuestPage searchGuest(String name) {
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(name);
        driver.findElement(By.cssSelector(".el-input:nth-child(2) > .el-input__inner")).sendKeys(Keys.ENTER);
        return this;
    }

    public GuestPage changeBlockState() {
        driver.findElement(By.cssSelector(".el-button--default")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".el-button--default"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        return this;
    }

    public GuestPage guestState(String state) {
        waitForWindow(1000);
        String currState = driver.findElement(By.cssSelector(".el-table__row:nth-child(1) > .el-table_2_column_14 span")).getText();
        assertThat(currState, is(state));
        return this;
    }
}
